<?php
/**
 *
 * @author LI
 *
 */
class HttpUtils {
	/**
	 *
	 * @param unknown $url        	
	 * @param unknown $method        	
	 * @param unknown $data        	
	 * @return mixed
	 */
	public static function invoke($url, $method, $data) {
		$ci = curl_init ();
		curl_setopt ( $ci, CURLOPT_USERAGENT, 'cURL 7.30.0' );
		curl_setopt ( $ci, CURLOPT_CONNECTTIMEOUT, 3 );
		curl_setopt ( $ci, CURLOPT_TIMEOUT, 30 );
		curl_setopt ( $ci, CURLOPT_RETURNTRANSFER, true );
		curl_setopt ( $ci, CURLOPT_SSL_VERIFYPEER, false );
		curl_setopt ( $ci, CURLOPT_SSL_VERIFYHOST, false );
		curl_setopt ( $ci, CURLOPT_HEADER, false );
		
		switch (strtoupper ( $method )) {
			case HttpMethod::$GET :
				$url = $url . '?' . HttpUtils::getParamString ( $data );
				break;
			case HttpMethod::$POST :
				curl_setopt ( $ci, CURLOPT_POST, TRUE );
				curl_setopt ( $ci, CURLOPT_POSTFIELDS, $data );
				break;
			default :
				;
				break;
		}
		
		curl_setopt ( $ci, CURLINFO_HEADER_OUT, TRUE );
		curl_setopt ( $ci, CURLOPT_URL, $url );
		
		$response = curl_exec ( $ci );
		curl_close ( $ci );
		
		return $response;
	}
	
	/**
	 *
	 * @param unknown $data        	
	 */
	public static function getParamString($data) {
		$paramString = "";
		
		while ( list ( $key, $val ) = each ( $data ) ) {
			$paramString .= '&' . $key . '=' . $val;
		}
		
		return substr ( $paramString, 1 );
		// while ( $key = key ( $test ) ) {
		// echo $key . $test [$key] . "<br />";
		// next ( $test );
		// }
	}
}

/**
 *
 * @author LI
 *        
 */
class HttpMethod {
	public static $GET = "GET";
	public static $POST = "POST";
}